package edu.njust.cma.controller;

import edu.njust.cma.pojo.Result;
import edu.njust.cma.pojo.User;
import edu.njust.cma.service.UserService;
import edu.njust.cma.util.AliOSSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/user")
public class UserController {
    /**
     * **用户**
     *
     * 0.查看个人信息
     * 1.修改个人密码
     * 2.修改名字，邮箱，电话，简历
     * 3.查看所有人的信息
     * 4.上传文件修改个人简历
     * 5.查看不同级别的所有人的信息
     */
    @Autowired
    UserService userService;
    @Autowired
    AliOSSUtils aliOSSUtils;
    /**
     * 0. 查看个人信息
     * @return user_info
     */
    @GetMapping("/info/{id}")
    public Result getUserInfo(@PathVariable Integer id){
        User user = userService.getUserInfo(id);
        return Result.success(user);
    }
    /**
     * 1. 修改个人密码
     * @param id
     * @param password
     * @return
     */
    @GetMapping("/password/{id}/{password}")
    public Result changePassword(@PathVariable Integer id, @PathVariable String password){
        userService.changePassword(id, password);
        return Result.success();
    }
    /**
     * 2. 修改名字，邮箱，电话
     * @param id
     * @param name
     * @param email
     * @param phone
     * @return
     */
    @GetMapping("/info/{id}/{name}/{email}/{phone}")
    public Result changeUserInfo(@PathVariable Integer id, @PathVariable String name, @PathVariable String email, @PathVariable String phone){
        userService.changeUserInfo(id, name, email, phone);
        return Result.success();
    }

    /**
     * 3. 查看所有人的信息
     * @return
     */
    @GetMapping("/all")
    public Result getAllUserInfo(){
        return Result.success(userService.getAllUserInfo());
    }
    /**
     * 4. 上传文件修改个人简历
     * @param id
     * @param resume
     * @return
     */
    @PostMapping("/resume/{id}")
    public Result changeResume(@PathVariable Integer id, @RequestBody MultipartFile resume) throws IOException {
        String resumeUrl = aliOSSUtils.upload(resume);
        userService.changeResume(id, resumeUrl);
        return Result.success();
    }
    /**
     * 5. 查看不同级别的所有人的信息
     * @param level
     * @return
     */
    @GetMapping("/level/{level}")
    public Result getUserInfoByLevel(@PathVariable Integer level){
        return Result.success(userService.getUserInfoByLevel(level));
    }
    /**
     * 6. 修改user的等级
     * @param userId
     * @param level
     * @return
     */
    
    @PostMapping("/level/{userId}/{level}")
    public Result changeUserLevel(@PathVariable Integer userId, @PathVariable Integer level){
        userService.changeUserLevel(userId, level);
        return Result.success();
    }
    /**
     * 7. 删除user
     * @param userId
     * @return
     */
    @DeleteMapping("/delete/{userId}")
    public Result deleteUser(@PathVariable Integer userId){
        userService.deleteUser(userId);
        return Result.success();
    }
    
    /**
     * 8. 添加user
     * @return
     */
    @PostMapping("/add/{name}/{level}")
    public Result addUser(@PathVariable String name, @PathVariable Integer level){
        User user = new User();
        user.setName(name);
        user.setLevel(level);
        return Result.success(userService.addUser(user));
    }
    /**
    * 9. 登录
     */
    @GetMapping("/login/{work_number}/{password}")
    public Result login(@PathVariable String work_number, @PathVariable String password){
        User user = userService.login(work_number, password);
        if(user == null){
            return Result.error("用户名或密码错误");
        }else{
            return Result.success(user);
        }
    }

}
